package com.IntelligentCommunity.deploy.controller;

import com.IntelligentCommunity.common.core.domain.R;
import com.IntelligentCommunity.common.core.utils.poi.ExcelUtil;
import com.IntelligentCommunity.common.core.web.controller.BaseController;
import com.IntelligentCommunity.common.core.web.domain.AjaxResult;
import com.IntelligentCommunity.common.core.web.page.TableDataInfo;
import com.IntelligentCommunity.common.log.annotation.Log;
import com.IntelligentCommunity.common.log.enums.BusinessType;
import com.IntelligentCommunity.common.security.annotation.RequiresPermissions;
import com.IntelligentCommunity.deploy.cache.TaskFaceCache;
import com.IntelligentCommunity.deploy.domain.DeployTaskFace;
import com.IntelligentCommunity.deploy.domain.vo.DeployTaskFaceVO;
import com.IntelligentCommunity.deploy.service.IDeployTaskFaceService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 抓拍任务Controller
 *
 * @author kwzjx
 * @date 2024-07-23
 */
@RestController
@RequestMapping("/face")
public class DeployTaskFaceController extends BaseController
{
    @Autowired
    private IDeployTaskFaceService deployTaskFaceService;

    /**
     * 查询抓拍任务列表
     */
    @RequiresPermissions("deploy:face:list")
    @GetMapping("/list/{flag}")
    public TableDataInfo list(DeployTaskFace deployTaskFace,
                              @PathVariable("flag") Integer flag,
                              @RequestParam(value = "populationName", required = false) String populationName,
                              @RequestParam(value = "idCardNumber", required = false) String idCardNumber)
    {
        startPage();
        List<DeployTaskFaceVO> list = deployTaskFaceService.selectDeployTaskFaceVOList(deployTaskFace, flag, populationName, idCardNumber);
        return getDataTable(list);
    }

    /**
     * 导出抓拍任务列表
     */
    @RequiresPermissions("deploy:face:export")
    @Log(title = "抓拍任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeployTaskFace deployTaskFace)
    {
        List<DeployTaskFace> list = deployTaskFaceService.selectDeployTaskFaceList(deployTaskFace);
        ExcelUtil<DeployTaskFace> util = new ExcelUtil<DeployTaskFace>(DeployTaskFace.class);
        util.exportExcel(response, list, "抓拍任务数据");
    }

    /**
     * 获取抓拍任务详细信息
     */
    @RequiresPermissions("deploy:face:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(deployTaskFaceService.selectDeployTaskFaceById(id));
    }

    /**
     * 新增抓拍任务
     */
    @RequiresPermissions("deploy:face:add")
    @Log(title = "抓拍任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeployTaskFace deployTaskFace)
    {
        return toAjax(deployTaskFaceService.insertDeployTaskFace(deployTaskFace));
    }

    /**
     * 修改抓拍任务
     */
    @RequiresPermissions("deploy:face:edit")
    @Log(title = "抓拍任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeployTaskFace deployTaskFace)
    {
        return toAjax(deployTaskFaceService.updateDeployTaskFace(deployTaskFace));
    }

    /**
     * 删除抓拍任务
     */
    @RequiresPermissions("deploy:face:remove")
    @Log(title = "抓拍任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deployTaskFaceService.deleteDeployTaskFaceByIds(ids));
    }

    /**
     * 部署人脸
     * @param deployTaskFace
     * @return
     */
    @PostMapping("/deployAdd")
    public R deployTaskFaceAdd(@RequestBody DeployTaskFace deployTaskFace) {
        deployTaskFaceService.deployTaskFaceAdd(deployTaskFace);
        return R.ok();
    }


    @GetMapping("/getTaskFaceCacheSize")
    public R<Long> getTaskFaceCacheSize() {
        Long taskNum = Long.valueOf(TaskFaceCache.getTaskFaceCacheSize());
        return R.ok(taskNum);
    }


    /**
     * 暂停/开始任务
     */
    @PostMapping("/pauseOrStart")
    public R pauseOrStart(
            @RequestParam("subject") String subject,
            @RequestParam("id") Long id,
            @RequestParam("status") Integer status) {
        deployTaskFaceService.pauseOrStart(subject, id, status);
        return R.ok();
    }

    /** 获取布控人脸 */
    @GetMapping("/getTaskFace/{id}")
    public R getTaskFace(@PathVariable("id") Long id) {
        List<String> images = deployTaskFaceService.selectTaskImagesById(id);
        return R.ok(images);
    }

    /** 根据id获取任务详情 */
    @GetMapping("/getTaskFaceDetail/{id}")
    public R getTaskFaceDetail(@PathVariable("id") Long id) {
        DeployTaskFaceVO deployTaskFaceVO = deployTaskFaceService.selectDeployTaskDetailById(id);
        return R.ok(deployTaskFaceVO);
    }

    /**实有人口布控*/
    @PostMapping("/deployPopulationAdd/{id}")
    public R deployPopulationAdd(@RequestBody DeployTaskFace deployTaskFace, @PathVariable("id") Long id) {
        deployTaskFaceService.deployPopulationAdd(deployTaskFace, id);
        return R.ok();
    }


}
